const initialState = {
  // 2. 从缓存中取出，作为初始值
  list: JSON.parse(localStorage.getItem('todo-list')) || [],
  type: localStorage.getItem('todo-type') || 'all',
};

export default function reducer(state = initialState, { type, payload }) {
  console.warn('type, payload进入仓库了  ----->  ', type, payload);
  switch (type) {
    case 'del':
      return {
        ...state,
        list: state.list.filter((item) => item.id !== payload),
      };

    case 'updateById':
      return {
        ...state,
        list: state.list.map((item) => {
          return { ...item, isDone: item.id === payload ? !item.isDone : item.isDone };
        }),
      };
    case 'clearDone':
      return { ...state, list: state.list.filter((item) => !item.isDone) };

    case 'updateType': {
      return { ...state, type: payload };
    }

    case 'add': {
      return {
        ...state,
        list: [payload, ...state.list],
      };
    }
    default:
      return state;
  }
}
